package servlet;

import com.alibaba.fastjson.JSON;
import pojo.User;
import service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * 用户登录验证
 * 输入一个用户对象user，返回验证结果true或者false
 * 用户不存在以及密码错误统统按照登录失败处理
 */
@WebServlet("/userLoginServlet")
public class UserLoginServlet extends HttpServlet {
    private final UserService userService = new UserService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        String params = reader.readLine();
        User user = JSON.parseObject(params, User.class);

        String jsonString;
        jsonString = JSON.toJSONString(userService.userLogin(user));
        response.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
